from django import forms
from web.forms.project import BootstragForm
from scripts.init_price_police import S28
from django.core.exceptions import ValidationError

class FileFildeForm(BootstragForm, forms.Form):
    def __init__(self, request, father, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.request = request
        self.father = father
        print('**file传过来的request', request.obj[0][0])
        print('**file传过来的father', father)

    file_name = forms.CharField(max_length=64, label='文件名')
    def clean_file_name(self):
        name = self.cleaned_data.get('file_name')
        print('我现在在form表单里的钩子函数 我看看name值', name)

        # 判断当前目录下的文件夹是否存在
        if self.father == None:
            ret = S28().search('file_repository', {'project_id': self.request.obj[0][0], 'name': name, 'type': '1', 'father': '0'})

        else:
            ret = S28().search('file_repository', {'project_id': self.request.obj[0][0], 'name': name, 'type': '1', 'father': self.father})
            print('当前的文件夹是否重复！！！', ret)
        if len(ret) != 0:
            raise ValidationError('重复的文件夹')

        return name
